﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _25_Mediator
{
    /* 
      抽象中介者定义了同事对象到中介者对象的接口
      */
    abstract class Mediator
    {
        public abstract void Send(string message, Colleague colleague);
    }

    /*
       Colleague 类 抽象同事类
     */
    abstract class Colleague
    {
        protected Mediator mediator;
        public Colleague(Mediator mediator)
        {
            this.mediator = mediator;
        }
    }

    /*
     * 具体中介类
     */
    class ConcreteMediator : Mediator
    {
        private ConcreteColleague1 colleague1;
        private ConcreteColleague2 colleague2;

        public ConcreteColleague1 Colleague1
        {
            set { colleague1 = value; }
        }
        public ConcreteColleague2 Colleague2
        {
            set { colleague2 = value; }
        }

        public override void Send(string message, Colleague colleague)
        {
            if (colleague == colleague1)
            {
                colleague2.Notify(message);
            }
            else
            {
                colleague1.Notify(message);
            }
        }
    }

    class ConcreteColleague1 : Colleague
    {
        public ConcreteColleague1(Mediator mediator) : base(mediator)
        {
        }

        internal void Notify(string message)
        {
            Console.WriteLine("同事1得到消息：" + message);
        }
        public void Send(string message)
        {
            mediator.Send(message,this);
        }
    }

    class ConcreteColleague2 : Colleague
    {
        public ConcreteColleague2(Mediator mediator) : base(mediator)
        {
        }

        internal void Notify(string message)
        {
            Console.WriteLine("同事2得到消息：" + message);
        }
        public void Send(string message)
        {
            mediator.Send(message, this);
        }
    }
}
